package com.platform.modules.alg.alglib.poj1579;

public class Poj1759 {
    public String output = "";

    private static int inf = 0x3f3f3f3f;
    private static int maxn = 1006;
    private static double eps = 1e-7;

    private int n;
    double A;
    double num[] = new double[maxn];
    Double ans;

    // 判断第二个灯的高度为 mid 是否可行
    boolean check(double mid) {
        num[2] = mid;
        for (int i = 3; i <= n; i++) {
            num[i] = 2 * num[i - 1] - num[i - 2] + 2;
            if (num[i] < eps) return false; // 写小于 0 由于精度问题会出现问题
        }
        ans = num[n];
        return true;
    }

    void solve() {
        num[1] = A;
        double l = 0.0;
        double r = A;
        while (r - l > eps) {
            double mid = (l + r) / 2;
            if (check(mid))
                r = mid;
            else
                l = mid;
        }
    }

    public String cal(String input) {
        String[] words = input.split(" ");
        n = Integer.parseInt(words[0]);
        A = Double.parseDouble(words[1]);
        solve();
        output = String.format("%.2f", ans);
        return output;
    }
}
